package com.czl.module_storehouse.activity.receive.details;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czl.base.data.bean.tengyun.ReceiveBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.utils.ParcelHelper;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.base.BaseOperationActivity;
import com.czl.module_storehouse.activity.receive.change.ReceiveChangeActivity;
import com.czl.module_storehouse.adapter.ReceiveAddPostAdapter;
import com.czl.module_storehouse.bean.ReceiveInfoBean;
import com.czl.module_storehouse.databinding.ActivityReceiveAddBinding;
import com.czl.module_storehouse.databinding.HeaderReceiveDetailsBinding;
import com.czl.module_storehouse.event.DetailsPostSuccessEvent;
import com.czl.module_storehouse.event.ReceiveChangePostEvent;
import com.czl.module_storehouse.event.ReceiveChangeSortEvent;
import com.czl.module_storehouse.event.ReceiveEvent;
import com.czl.module_storehouse.mvp.presenter.ReceivePresenter;
import com.czl.module_storehouse.mvp.view.ReceiveView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author xubin
 */
public class ReceiveDetailsPostActivity extends BaseOperationActivity<ActivityReceiveAddBinding> implements ReceiveView {

    private final ReceiveInfoBean mReceiveInfoBean = new ReceiveInfoBean();

    private List<SortBean> mRemoveSortList;
    private ReceiveAddPostAdapter mAdapter;
    private HeaderReceiveDetailsBinding mDetailsBinding;
    @InjectPresenter
    ReceivePresenter mReceivePresenter;

    @Override
    protected ActivityReceiveAddBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityReceiveAddBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("领用出库");
        binding.btnSure.setText("领用出库");
        binding.recyclerView.setAdapter(getAdapter());
    }

    @Override
    protected BaseQuickAdapter<SortBean, ?> getAdapter() {
        if (mAdapter == null) {
            mAdapter = new ReceiveAddPostAdapter(new ArrayList<>());
            mDetailsBinding = HeaderReceiveDetailsBinding.inflate(getLayoutInflater());
            mAdapter.addHeaderView(mDetailsBinding.getRoot());
            addFooterView(mAdapter);
            mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                if (R.id.text_change == view.getId()) {
                    nextChangeActivity(position - mAdapter.getHeaderLayoutCount());
                } else if (R.id.cl_root == view.getId()) {
                    startActivity(position - mAdapter.getHeaderLayoutCount());
                }
            });
            mAdapter.setOnItemClickListener((adapter, view, position) -> {
                mSelectedPosition = position - mAdapter.getHeaderLayoutCount();
                SortBean sortBean = mAdapter.getItem(position);
                if (sortBean == null) {
                    return;
                }
            /*if (!sortBean.isReceived()) {
                return;
            }*/
                startActivity(position);
            });
        }
        return mAdapter;
    }

    @Override
    protected Button getButton() {
        return binding.btnSure;
    }

    @Override
    protected String getOperationTitle() {
        return "领用";
    }

    @Override
    protected int getMaxNum(SortBean sortBean) {
        return sortBean.getHandleNumInt();
    }

    private void nextChangeActivity(int position) {
        SortBean item = mAdapter.getItem(position);
        EventBus.getDefault().postSticky(new ReceiveChangeSortEvent(item));
        startActivity(new Intent(getContext(), ReceiveChangeActivity.class));
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(ReceiveChangePostEvent event) {
        List<SortBean> sortBeans = event.getSortBeans();
        if (sortBeans == null) {
            return;
        }
        List<SortBean> data = mAdapter.getData();
        SortBean eventSortBean = event.getSortBean();
        List<SortBean> newList = new ArrayList<>(sortBeans);
        eventSortBean.setChangeSortList(sortBeans);
        for (int i = 0; i < data.size(); i++) {
            SortBean sortBean = data.get(i);
            List<SortBean> removeSortBeans = event.getRemoveSortBeans();
            if (removeSortBeans != null) {
                for (SortBean removeSort : removeSortBeans) {
                    if (Objects.equals(removeSort.getSortId(), sortBean.getSortId())) {
                        mAdapter.remove(sortBean);
                    }
                }
            }
            if (Objects.equals(eventSortBean.getSortId(), sortBean.getSortId())) {
                mAdapter.setData(i, eventSortBean);
                continue;
            }
            for (SortBean bean : sortBeans) {
                if (Objects.equals(sortBean.getSortId(), bean.getSortId())) {
                    mAdapter.setData(i, bean);
                    newList.remove(bean);
                    break;
                }
            }
        }
        mAdapter.addData(newList);
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(ReceiveEvent event) {
        ReceiveBean receiveBean = event.getReceiveBean();
        //拷贝数据
        ReceiveBean copy = ParcelHelper.copy(receiveBean);

        List<SortBean> sortList = copy.getSortList();
        if (sortList == null) {
            return;
        }
        mRemoveSortList = new ArrayList<>();
        List<SortBean> newList = new ArrayList<>(sortList);
        for (SortBean sortBean : sortList) {
            if (sortBean.getFirstFacilityCount() == null || sortBean.getFirstFacilityCount() == 0) {
                mRemoveSortList.add(sortBean);
                newList.remove(sortBean);
            }
        }
        mAdapter.setList(newList);

        initHeaderView(receiveBean);
        mReceiveInfoBean.setReceiveBean(receiveBean);
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(ReceiveBean receiveBean) {
        if (receiveBean == null) {
            return;
        }
        mDetailsBinding.clHeaderInfo.tvHeaderTitle.setText(receiveBean.getReceiveName());
        mDetailsBinding.clHeaderInfo.tvHeaderDate.setText(receiveBean.getReceiveDate());
        mDetailsBinding.clHeaderInfo.tvHeaderCode.setText("领用单：" + (receiveBean.getReceiveCode() == null ? "" : receiveBean.getReceiveCode()));
        mDetailsBinding.tvHeaderDepartment.setText("领用部门：" + (receiveBean.getReceivePersonDepartment() == null ? "" : receiveBean.getReceivePersonDepartment()));
        mDetailsBinding.tvHeaderDesc.setText("领用原因：" + (receiveBean.getApplyComment() == null ? "" : receiveBean.getApplyComment()));
    }

    /**
     * 提交
     */
    @Override
    protected void submit() {
        List<SortBean> newList = getNewList();
        if (mRemoveSortList != null) {
            newList.addAll(mRemoveSortList);
        }
        mReceiveInfoBean.setSortList(newList);
        String remark = mFooterBinding.etRemark.getText().toString();
        mReceiveInfoBean.setReceiveComment(remark);
        if (mReceivePresenter != null) {
            mReceivePresenter.addReceiveInfo(mReceiveInfoBean);
        }
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);

    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (ReceivePresenter.TAG_RECEIVE_ADD_INFO.equals(httpResponse.getRequestTag())) {
            showToast("领用成功");
            LiveEventBus.get(DetailsPostSuccessEvent.class).post(new DetailsPostSuccessEvent());
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDetailsBinding != null) {
            mDetailsBinding.unbind();
        }
    }
}
